package com.stylefeng.guns.modular.platform.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.NoGenerateUtils;
import com.stylefeng.guns.modular.platform.model.OrderInfo;
import com.stylefeng.guns.modular.platform.service.IOrderInfoService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-07-13 19:10:38
 */
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController {

	private String PREFIX = "/platform/orderInfo/";

	@Autowired
	private IOrderInfoService orderInfoService;

	/**
	 * 跳转到首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "orderInfo.html";
	}

	/**
	 * 跳转到添加
	 */
	@RequestMapping("/orderInfo_add")
	public String orderInfoAdd() {
		return PREFIX + "orderInfo_add.html";
	}

	/**
	 * 跳转到修改
	 */
	@RequestMapping("/orderInfo_update/{orderInfoId}")
	public String orderInfoUpdate(@PathVariable Integer orderInfoId, Model model) {
		OrderInfo orderInfo = orderInfoService.selectById(orderInfoId);
		model.addAttribute("item", orderInfo);
		LogObjectHolder.me().set(orderInfo);
		return PREFIX + "orderInfo_edit.html";
	}

	/**
	 * 获取列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(OrderInfo orderInfo) {

		Page<OrderInfo> page = new PageFactory<OrderInfo>().defaultPage();
		page.setOrderByField("id");
		page.setAsc(false);
		List<OrderInfo> lsit = orderInfoService.getList(page, orderInfo);
		page.setRecords(lsit);
		PageInfoBT<OrderInfo> d = super.packForBT(page);
		return d;

	}

	/**
	 * 获取列表
	 */
	@RequestMapping(value = "/getStatistics")
	@ResponseBody
	public Object getStatistics(OrderInfo orderInfo) {

		Map<String, Object> map = orderInfoService.getStatistics(orderInfo);

		return map;

	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(OrderInfo orderInfo) {
		orderInfo.setOrderId(NoGenerateUtils.getNo());
		orderInfo.setCreatedBy("admin");
		orderInfo.setGmtCreate(new Date());
		orderInfo.setGmtLastUpdate(orderInfo.getGmtCreate());
		orderInfo.setAmount(orderInfo.getAmount() * 100);
		if ("1".equals(orderInfo.getStatus().toString())) {
			orderInfo.setPayTime(DateUtil.getTime());
		}
		orderInfoService.insert(orderInfo);
		return SUCCESS_TIP;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Integer orderInfoId) {
		orderInfoService.deleteById(orderInfoId);
		return SUCCESS_TIP;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(OrderInfo orderInfo) {
		int amount = orderInfo.getAmount() * 100;
		orderInfo.setAmount(amount);
		if (StringUtils.isEmpty(orderInfo.getPayTime()) && "1".equals(orderInfo.getStatus().toString())) {
			orderInfo.setPayTime(DateUtil.getTime());
		}
		orderInfoService.updateById(orderInfo);
		return SUCCESS_TIP;
	}

	/**
	 * 详情
	 */
	@RequestMapping(value = "/detail/{orderInfoId}")
	@ResponseBody
	public Object detail(@PathVariable("orderInfoId") Integer orderInfoId) {
		return orderInfoService.selectById(orderInfoId);
	}
}
